package cn.edu.xidian.untils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

public class PermissionUntils{

    String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};
    List<String> PermissionList = new ArrayList<>();

    // private ImageView welcomeImg = null;
    public static final int PERMISSION_REQUEST = 1;

    public PermissionUntils() {
    }

    // 检查权限
    public void checkPermission(Activity activity,Context context) {
        PermissionList.clear();
        //判断哪些权限未授予
        for (int i = 0; i < permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(context, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                PermissionList.add(permissions[i]);
            }
        }
        /**
         * 判断是否为空
         */
        if (PermissionList.isEmpty()) {//未授予的权限为空，表示都授予了
        } else {//请求权限方法
            String[] permissions = PermissionList.toArray(new String[PermissionList.size()]);//将List转为数组
            ActivityCompat.requestPermissions(activity, permissions, PERMISSION_REQUEST);
        }
    }

}
